package servlet.task;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import beans.Info;
import beans.Task;
import service.TaskService;

@WebServlet("/TaskServlet")
public class TaskServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public TaskServlet() {
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		TaskService service = new TaskService();

		String page = request.getParameter("page"); // 第几页
		if (page == null)
			page = "1";
		Info<Task> info = null;
		String id = request.getParameter("id"); // 要查询的id
		if (id != null && !id.equals("")) {
			request.setAttribute("id", id);
			info = service.getTaskById(id);
		}else {
			info = service.getTask(page);
		}

		request.setAttribute("tasks", info.list); // 任务信息列表
		request.setAttribute("page", page);
		request.setAttribute("count", info.count);
		request.getRequestDispatcher("/admin/task.jsp").forward(request, response);
	}
}
